{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fzRFGWxAsTm2"
      },
      "source": [
        "##### Copyright 2025 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "Y0nQsAf2sSfs"
      },
      "outputs": [],
      "source": [
        "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sP8PQnz1QrcF"
      },
      "source": [
        "# Gemini API: Chain of thought prompting"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bxGr_x3MRA0z"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Chain_of_thought_prompting.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ysy--KfNRrCq"
      },
      "source": [
        "Using chain of thought helps the LLM take a logical and arithmetic approach. Instead of outputting the answer immediately, the LLM uses smaller and easier steps to get to the answer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "Ne-3gnXqR0hI"
      },
      "outputs": [],
      "source": [
        "%pip install -U -q \"google-genai>=1.0.0\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eomJzCa6lb90"
      },
      "source": [
        "## Configure your API key\n",
        "\n",
        "To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "v-JZzORUpVR2"
      },
      "outputs": [],
      "source": [
        "from google.colab import userdata\n",
        "from google import genai\n",
        "\n",
        "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
        "client = genai.Client(api_key=GOOGLE_API_KEY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DbEKYMrKnA-C"
      },
      "source": [
        "Additionally, select the model you want to use from the available options below:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "Pltkz-0UnDvD"
      },
      "outputs": [],
      "source": [
        "MODEL_ID = \"gemini-2.5-flash\"  # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-flash\",\"gemini-2.5-pro\",\"gemini-3-pro-preview\"] {\"allow-input\": true, \"isTemplate\": true}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LlRYq9JSeRzR"
      },
      "source": [
        "## Example\n",
        "\n",
        "Sometimes LLMs can return non-satisfactory answers. To simulate that behavior, you can implement a phrase like \"Return the answer immediately\" in your prompt.\n",
        "\n",
        "Without this, the model sometimes uses chain of thought by itself, but it is inconsistent and does not always result in the correct answer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "0u1IjBOmeQgG"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "5 minutes\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from IPython.display import Markdown\n",
        "\n",
        "prompt = \"\"\"\n",
        "  5 people can create 5 donuts every 5 minutes. How much time would it take\n",
        "  25 people to make 100 donuts? Return the answer immediately.\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kCFJ2dvBubSm"
      },
      "source": [
        "To influence this you can implement chain of thought into your prompt and look at the difference in the response. Note the multiple steps within the prompt."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "HdMNHASyeWoK"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Here's how to solve the donut problem:\n\n*   **Donuts per person per minute:** If 5 people make 5 donuts in 5 minutes, then one person makes one donut in 5 minutes (5 donuts / 5 people = 1 donut per person).\n*   **Donuts by 25 people in 5 minutes:** 25 people can make 25 donuts every 5 minutes (25 people * 1 donut/person = 25 donuts).\n*   **How many 5-minute intervals?** To make 100 donuts, it would take four 5-minute intervals (100 donuts / 25 donuts per interval = 4 intervals).\n*   **Total Time:** 4 intervals * 5 minutes/interval = 20 minutes.\n\n**Answer:** It would take 25 people 20 minutes to make 100 donuts.",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "  Question: 11 factories can make 22 cars per hour. How much time would it take 22 factories to make 88 cars?\n",
        "  Answer: A factory can make 22/11=2 cars per hour. 22 factories can make 22*2=44 cars per hour. Making 88 cars would take 88/44=2 hours. The answer is 2 hours.\n",
        "  Question: 5 people can create 5 donuts every 5 minutes. How much time would it take 25 people to make 100 donuts?\n",
        "  Answer:\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w_K9Sn3Yu9eL"
      },
      "source": [
        "## Next steps\n",
        "\n",
        "Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as few-shot prompting."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "Chain_of_thought_prompting.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
